package com.team.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.team.dao.ContactDao;
import com.team.entity.Contact;
import com.team.entity.Project;
import com.team.util.Const;
import com.team.util.StringUtil;

public class NewContactServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;
	public void service(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		String method = request.getParameter("_m");
		
		if("save".equals(method)){
			save(request,response);
		}else if("select".equals(method)){
			select(request,response);
		}else if("find".equals(method)){
			findContact(request,response);
			
		}else if("edit".equals(method)){
			editContact(request,response);
		}else if("update".equals(method)){
			updateContact(request,response);
		}else {
			response.sendError(404,"request a error path");
			return ;
		}
		
		
		
		
	}
	
	private void updateContact(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String conId = request.getParameter("conId");
		int id = Integer.parseInt(conId);
		String name = request.getParameter("name");
		String company = request.getParameter("company");
		String mobel = request.getParameter("mobel");
		String tel = request.getParameter("tel");
		String email = request.getParameter("email");
		String address = request.getParameter("address");
		String websate = request.getParameter("websate");
		String weibo = request.getParameter("weibo");
		String content = request.getParameter("content");
		
		if(StringUtil.isEmpty(content)||StringUtil.isEmpty(weibo)
				||StringUtil.isEmpty(websate)||StringUtil.isEmpty(address)
				||StringUtil.isEmpty(email)||StringUtil.isEmpty(tel)
				||StringUtil.isEmpty(mobel)||StringUtil.isEmpty(company)
				||StringUtil.isEmpty(name)){
			
			response.sendRedirect("editcontact.jsp?code=1001");
			return ;
		}else {
			HttpSession session = request.getSession();
			Project proj = (Project) session.getAttribute(Const.SESSION_PROJ);
			
			Contact con = new Contact();
			con.setAddress(address);
			con.setCompanyName(company);
			con.setContactName(name);
			con.setContent(content);
			con.setEmail(email);
			con.setId(id);
			con.setMobelNum(mobel);
			con.setProjId(proj.getId());
			con.setTelNum(tel);
			con.setWebSate(websate);
			con.setWeiBo(weibo);
			
			ContactDao cd = new ContactDao();
			int num = cd.update(con);
			if(num > 0){
				System.out.println("contanct edit update succ");
				response.sendRedirect("newContact.jspx?_m=select&projId="+proj.getId());
				return ;
			}else {
				System.out.println("contanct edit update error");
			}
			
		}
		
		
	}

	private void editContact(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		String conId = request.getParameter("conId");
		int id = Integer.parseInt(conId);
		
		System.out.println("conid\t"+id);
		ContactDao cd = new ContactDao();
		Contact temp = cd.selectByContactId(id);
		if(temp != null){
			System.out.println("find contact edit succ");
			HttpSession session = request.getSession();
			session.setAttribute(Const.SESSION_EDITCON, temp);
		}else {
			System.out.println(id+"~contact isn't exists");
		}
		response.sendRedirect("editcontact.jsp");
		return;
	}

	private void findContact(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String contanctId = request.getParameter("contactId");
		int id = Integer.parseInt(contanctId);
		ContactDao cd = new ContactDao();
		Contact ct = cd.selectByContactId(id);
		if(ct != null){
			HttpSession session = request.getSession();
			session.setAttribute(Const.SESSION_CONTACT_RECORED, ct);
			System.out.println("turn to record.jspx");
			response.sendRedirect("record.jspx?_m=find&conId="+id);
			return ;
		}else {
			System.out.println("联系人不存在");
			response.sendRedirect("contact.jsp");
			return;
		}
	}

	private void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		String  id= request.getParameter("projId");
		System.out.println("projId"+id);
		int projId = Integer.parseInt(id);
		
		ContactDao dao = new ContactDao();
		
		List<Contact> contactList = dao.findAllByProjId(projId);
		
		if(contactList !=null){
			HttpSession session = request.getSession();
			session.setAttribute(Const.SESSION_CONTACT,contactList);
			response.sendRedirect("contact.jsp");
			return ;
		}else{
			System.out.println("error");
			response.sendRedirect("newContact.jsp");
			return ;
		}
	}

	
	private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String name = request.getParameter("name");
		String companyName=request.getParameter("company");
		String mobel=request.getParameter("mobel");
		String tel= request.getParameter("tel");
		String address=request.getParameter("address");
		String email=request.getParameter("email");
		String kind=request.getParameter("kind");
		String websate=request.getParameter("websate");
		String text=request.getParameter("text");
		String content=request.getParameter("textarea");
		String weibo=kind+"@"+text;
		
		
		HttpSession session = request.getSession();
		Project project =  (Project) session.getAttribute(Const.SESSION_PROJ);
		
		int projId = project.getId();
		Contact contact = new Contact();
		contact.setAddress(address);
		contact.setCompanyName(companyName);
		contact.setContactName(name);
		contact.setEmail(email);
		contact.setMobelNum(mobel);
		contact.setTelNum(tel);
		contact.setWebSate(websate);
		contact.setWeiBo(weibo);
		contact.setContent(content);
		contact.setProjId(projId);
		
	
		ContactDao dao = new ContactDao();
		int num = dao.save(contact);
		if(num > 0){
			System.out.println("save contact succ");
			response.sendRedirect("newContact.jspx?_m=select&projId="+projId);
			return ; 
			
		}else{
			System.out.println("save contact error");
			response.sendRedirect("newContact.jsp");
			return ;
		}
		
	}

}
